package com.springcloud.gateway.provider;

import com.springcloud.gateway.props.RouteProperties;
import com.springcloud.gateway.props.RouteResource;
import lombok.AllArgsConstructor;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;

import java.util.ArrayList;
import java.util.List;

/**
 * @version V1.0
 * @Author 宋聚智
 * @Date 2019/11/4 11:51
 * @description
 * @Modify By：
 * @ModifyTime： 2019/11/4
 * @Modify marker：
 **/
@Primary
@Component
@AllArgsConstructor
public class SwaggerProvider implements SwaggerResourcesProvider {
    private static final String API_URI="v2/api-docs-ext";
    private RouteProperties routeProperties;
    @Override
    public List<SwaggerResource> get() {
        List<SwaggerResource> resourceList = new ArrayList<>();
        List<RouteResource> routeResourceList= routeProperties.getResourceList();
        routeResourceList.forEach(routeResource -> resourceList.add(swaggerResource(routeResource)));
        return  resourceList;
    }


    private SwaggerResource swaggerResource(RouteResource routeResource) {
        SwaggerResource swaggerResource = new SwaggerResource();
        swaggerResource.setName(routeResource.getName());
        swaggerResource.setLocation(routeResource.getLocation().concat(API_URI));
        swaggerResource.setSwaggerVersion(routeResource.getVersion());
        return swaggerResource;
    }
}
